package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.ThirdpartyService;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.mag.thirds.VehicleViolationBean;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/6/30.
 */
public class PlateEngineService extends BaseService<PlateEngineNumEntity> {
    private Context context;
    private PlateEngineNumEntity plateEngineNumEntity;
    private boolean isQuery = false;

    public PlateEngineService(Context context) {
        this.context = context;
    }

    @Override
    public PlateEngineNumEntity jsonToBean(String json) {
        if (!CommonUtil.isNull(json))
            return GsonHelper.fromJson(json,PlateEngineNumEntity.class);
        else return null;
    }

    @Override
    public RequestEntity getRequestEntity() {
        RequestEntity requestEntity= NetConnectUtil.getAuthRequestEntity(context);
        UsersService usersService = new UsersService(Constants.CURRENT_SERVER_URL);
        if (isQuery) {
            requestEntity = usersService.getVehicleInfo(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getVin());
            requestEntity = HttpInjectUtils.getDefaultConfig(Language.AUTO, HttpMethod.GET, HttpAccept.JSON, requestEntity);
        } else {
            requestEntity = usersService.putVehicleInfo(plateEngineNumEntity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getVin());
            requestEntity = HttpInjectUtils.getDefaultConfig(Language.AUTO, HttpMethod.PUT, HttpAccept.JSON, requestEntity);
        }
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);

        requestEntity = HttpInjectUtils.injectHttp(PlateEngineService.this.getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }


/*    public Observable<PlateEngineNumEntity> getObserval(){
        return Observable.defer(new Func0<Observable<PlateEngineNumEntity>>() {
            @Override
            public Observable<PlateEngineNumEntity> call() {
                try {
                    return Observable.just((PlateEngineNumEntity)JsonParseUtil.JsonToBean(PlateEngineNumEntity.class, BusinessUtil.getResponse(getDatas(getRequestEntity()), context)));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }*/

    public Observable<PlateEngineNumEntity> updateVehicleInfo(final PlateEngineNumEntity plateEngineNumEntity) {
        return Observable.defer(new Func0<Observable<PlateEngineNumEntity>>() {
            @Override
            public Observable<PlateEngineNumEntity> call() {
                try {
                    setPlateEngineNumEntity(plateEngineNumEntity, false);
                    if (plateEngineNumEntity.isUpdate()) {
                        RequestEntity requestEntity = getRequestEntity();
                        //因为保存接口未返回成功响应数据，此处不接收
                        BusinessUtil.getResponse(getDatas(requestEntity), context);
                    }
                    return Observable.just(plateEngineNumEntity);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public Observable<VehicleViolationBean> violationQuery(final PlateEngineNumEntity plateEngineNumEntity) {
        return Observable.defer(new Func0<Observable<VehicleViolationBean>>() {
            @Override
            public Observable<VehicleViolationBean> call() {
                try {
                    ThirdpartyService joinmeService = new ThirdpartyService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);

                    requestEntity = joinmeService.violationQuery(plateEngineNumEntity.getVin(), plateEngineNumEntity.getLicensePlate(), plateEngineNumEntity.getEngineNumber(), requestEntity);
                    requestEntity = HttpInjectUtils.injectHttp(PlateEngineService.this.getClass(), "violationQuery", requestEntity, PlateEngineNumEntity.class);

                    return Observable.just(GsonHelper.fromJson( BusinessUtil.getResponse(getDatas(requestEntity), context),VehicleViolationBean.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    public void setPlateEngineNumEntity(PlateEngineNumEntity plateEngineNumEntity, boolean isQuery) {
        this.plateEngineNumEntity = plateEngineNumEntity;
        this.isQuery = isQuery;
    }

}
